/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.matrices.lista;

import DBMS.DataBaseManager;
import admin.permisos.PermisosManager;
import clases.Lista;
import clases.Usuario;
import clases.Valor;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author andreth
 */
public class AgregarLista extends DispatchAction {

    /* forward name="success" path="" */
    private static final String AGREGARVALORES = "agregarvalores";
    private static final String FAILURE = "failure";
    private static final String POPULATE = "populate";
    private static final String name = "Agregar Lista";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward save(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        Lista l = (Lista) form;

        System.out.println("Tamano de la lista y size " + l.getNumeroValores() + " " + l.getValores().size());
        if (DataBaseManager.getInstance().agregarLista(l)) {
            l.setMensajeOK("Lista agregada");
            request.setAttribute("lista", l);
            System.out.println("Ejecutando agregacion de descripcion de lista existosa" + l.getIdLista());
            return mapping.findForward(AGREGARVALORES);
        } else {
            System.out.println("Ejecutando agregacion de descripcion de lista  fallida");
            l.setMensaje("Lista no agregada");
            return mapping.findForward(FAILURE);
        }
    }

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        return mapping.findForward(POPULATE);
    }
}
